﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PowerCreator.LMS.Course.Entity;
using PowerCreator.LMS.Web.Catalog;
using PowerCreator.LMS.Common.Script;

public partial class sysadmin_CourseManager_AddCatalog :PowerCreator.LMS.Web.Base.WebBasePage
{
    Catalog opCatalog = new Catalog();
    private PowerCreator.LMS.Web.DL.User.LoginEntity login = new PowerCreator.LMS.Web.DL.User.LoginEntity();
    private string url = "../login.aspx";

    protected void Page_Load(object sender, EventArgs e)
    {
        CheckUser();
        if (!IsPostBack)
        {
            InitPage();
            string catalogid = Request.QueryString["CatalogID"].ToString();
            ViewState["CatalogID"] = catalogid;

            //BinderDDL();
        }
    }
    private void CheckUser()
    {
        if (Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO] == null)
        {
            Script.RedirectNewUrl(url);
        }
        else
        {
            login = (PowerCreator.LMS.Web.DL.User.LoginEntity)Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO];
            if (login == null)
            {
                Script.RedirectNewUrl(url);
            }
            else
            {
                if (login.VOAdvancedUser == null)
                {
                    Script.RedirectNewUrl(url);
                }
                else if (login.VOAdvancedUser.AUserGroup > 2)
                {
                    //Response.Write(login.VOAdvancedUser.AUserGroup.ToString());
                    //Response.End();
                    Script.ShowAndHistory("您无此权限！", -1);
                }
            }

        }


    }

    protected override void InitPage()
    {
        lbl_InfoTitle.Text = GetLanageValueByKey("lbl_AddCatalog_InfoTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_Dep_Name.Text = GetLanageValueByKey("lbl_AddCatalog_Name", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_parentcatalog.Text = GetLanageValueByKey("lbl_AddCatalog_parentcatalog", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_catalog_description.Text = GetLanageValueByKey("lbl_AddCatalog_description", PowerCreator.LMS.Language.LanagetItemType.Page);
        
        rfv_CatalogName.ErrorMessage = GetLanageValueByKey("AddCatalog_msg_g1", PowerCreator.LMS.Language.LanagetItemType.Page);
        
        btn_Save.Text = GetLanageValueByKey("btn_Save", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_Reset.Text = GetLanageValueByKey("btn_Reset", PowerCreator.LMS.Language.LanagetItemType.Page);
        base.InitPage();
    }

    //private void BinderDDL()
    //{
    //    string search = "";
    //    DataTable dt = opCatalog.QueryCatalog(search);
    //    ArrayList list = opCatalog.GetCatalogTree(dt);
    //    ddl_Catalog.DataSource = list;
    //    ddl_Catalog.DataTextField = "Text";
    //    ddl_Catalog.DataValueField = "Value";
    //    ddl_Catalog.DataBind();
    //    ListItem li = new ListItem();
    //    li.Text = GetLanageValueByKey("AddCatalog_db_d1", PowerCreator.LMS.Language.LanagetItemType.Page);
    //    li.Value = "0";
    //    ddl_Catalog.Items.Insert(0, li);

    //    ddl_Catalog.SelectedValue = ViewState["CatalogID"].ToString();

    //}
    private bool CheckPageData()
    {
        string search = string.Format("CatalogName='{0}'", txt_CatalogName.Text);
        if (opCatalog.QueryCatalogCount(search) != 0)
        {
            //lbl_addcatalog_msg_catalogname.Text = "己经存在该专业信息，请重新更改专业名称!";
            lbl_msg_description.Text = GetLanageValueByKey("AddCatalog_msg_g2", PowerCreator.LMS.Language.LanagetItemType.Page);
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "showmy1", "<script>AutoColseAler()</script>");
            return false;
        }
        return true;
    }
    protected void btn_Save_Click(object sender, EventArgs e)
    {
        if (!CheckPageData()) return;

        VOTCatalog vo = new VOTCatalog();
        vo.CatalogName = txt_CatalogName.Text;
        vo.CatalogFatherId = 0;
        vo.CreatorId = GetLoginEntity.UserID;
        vo.CreatorName = GetLoginEntity.UserName;
        //vo.CatalogFatherId = int.Parse(ddl_Catalog.SelectedValue.ToString());
        vo.CatalogContent = txt_CatalogDescription.Text;
        vo.CatalogLayer = opCatalog.QueryDepartmentLayer(vo.CatalogFatherId);

        if (opCatalog.AddCatalog(vo))
        {
            string url = "Catalog.aspx";
            Script.RedirectURL(url);
        }
    }
    protected void btn_Back_Click(object sender, EventArgs e)
    {
        string url = "Catalog.aspx";
        Script.RedirectURL(url);
    }
}
